import api from '@/libs/request'
import { encryptAccept } from '@/utils/rsa'
import { GrantType, GrantTypes, SignResp } from '@/apis/sign/type'
import { CaptchaRequest, CaptchaResponse } from '@/components/slide-captcha/Captcha'
import storage from '@/utils/storage'

/**
 * 获取滑块验证码
 */
export const doGetCaptchaSlider = () => {
    return api.get<CaptchaResponse>(`gruul-mall-uaa/uaa/auth/captcha/slider`)
}

/**
 * 获取验证码
 */
export const doPostSmsCode = (data: CaptchaRequest<string>) => {
    encryptAccept((encrypt) => typeof data.form === 'string' && (data = { ...data, form: { mobile: encrypt(data.form) } }))
    return api.post(`gruul-mall-uaa/uaa/auth/captcha/sms`, data)
}

/**
 * 登录
 */
export const doSignByUser = (grantType: GrantType, data: any) => {
    const grant = GrantTypes[grantType]
    //参数加密
    if (grant.encrypt) {
        encryptAccept((encrypt) => {
            grant.encryptFields?.forEach((field: string) => {
                data[field] = encrypt(data[field])
            })
        })
    }
    return api.post<SignResp>(`gruul-mall-uaa/login?grant_type=${grant.value}`, data, { 'Shop-Id': 0 })
}
/**
 * 店铺记录访客数
 */
export const doAddVistor = () => {
    return api.post('gruul-mall-shop/shop/visitor/add')
}
/**
 * 获取会员中心信息
 */
export const doGetMemberCardInfo = () => {
    return api.get(`gruul-mall-user/user/member/card/info`)
}

/**
 * 退出登录
 */
export const doPostLogout = () => {
    storage.removeThis()
    return api.post('gruul-mall-uaa/logout')
}
